﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>多边形求交</title>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/dijit/themes/tundra/tundra.css"/>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/esri/css/esri.css" />
    <script  type="text/Javascript" src="http://localhost/arcgis_js_api/library/3.17/3.17/init.js"></script>
    <style type="text/css">
        .MapClass{
            width:100%;
            height:500px;
            border:1px solid #000;
        }
    </style>
    <script type="text/javascript">

        require(["esri/map",
			"dojo/dom","dojo/on","dojo/query",
            "esri/layers/ArcGISDynamicMapServiceLayer",
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/toolbars/draw",
            "esri/graphic",
            "esri/tasks/GeometryService",
			"dojo/colors",
            "dojo/domReady!"],
            function (Map,dom,on,query,
                ArcGISDynamicMapServiceLayer,
                SimpleMarkerSymbol,
                SimpleLineSymbol,
                SimpleFillSymbol,
                Draw,
                Graphic,
                GeometryService,
                Color) {
                var map = new esri.Map("mapDiv");
                var layer = new esri.layers.ArcGISDynamicMapServiceLayer
                ("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer");
                map.addLayer(layer);
				//创建几何服务对象
                var geometryServices = new GeometryService("http://localhost:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
				//一个标志位，用于记录是绘制第几个参数
				//1代表第一个参数
				//2代表第二个参数
				var flag;
				//第一个参数  注意第一个参数是多个形状
				var geometries=[];
				//第二个参数  注意第二个参数是一个形状
				var paramGeometry;
                //使用toolbar上的绘图工具
                var toolBar = new Draw(map);
				
                //线符号
                lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255, 0, 0]), 3);
                //第一个参数的面符号
                polygonSymbol1 = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol, new Color([255, 0, 0, 0.25]));
				//第二个参数的面符号
                polygonSymbol2 = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol, new Color([0, 255, 0, 0.25]));
				//求交之后的面符号
                polygonSymbol3 = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol, new Color([0, 0, 255, 0.25]));
				//给绘制polygon的按钮绑定事件
				query(".pbtn").on("click",function(event){
					var value=event.rangeParent.data;
					switch(value){
						case "第一个参数":{
							flag=1;
							break;
							
						}
						case "第二个参数":{
							flag=2;
							break;
						}	
					}
					//激活绘制多边形
					toolBar.activate(Draw.POLYGON, {
                        showTooltips:true
                    })
					
				})
				//绘图结束绑定事件
				on(toolBar,"draw-end",function(result){
					//获得面形状
					var geometry=result.geometry;
					//创建Graphic
					var graphicpoint;

					//很重要！！！在这里构建参数对象
					if(flag==1)
					{
						//创建Graphic
						graphicpoint = new Graphic(geometry, polygonSymbol1);
						geometries.push(geometry)
					}
					else if(flag==2)
					{
						//创建Graphic
						graphicpoint = new Graphic(geometry, polygonSymbol2);
						paramGeometry=geometry;
					}	
                    map.graphics.add(graphicpoint); 
					//关闭绘图工具
                    toolBar.deactivate();					
				})
				//给求交按钮绑定事件
                on(dom.byId("Btn"),"click",function(){
					//在这里在说一边，第一个参数是图形集合，第二个参数是一个图形
					geometryServices.intersect(geometries,paramGeometry,function(result){
						for(var idx in result)
						{
							//创建Graphic
							var graphic=new Graphic(result[idx],polygonSymbol3)
							//将每一个结果都放在map中
							map.graphics.add(graphic)
						}
					})
				})
               
            });
    </script>
</head>
<body>
    <div id="mapDiv" class="MapClass"></div>
	<button class="pbtn" >第一个参数</button>
    <button class="pbtn" >第二个参数</button>
    <input id="Btn" type="button" value="求交分析" />
</body>
</html>
